//
//  HomeTransforShowOrderInfoViewController.m
//  ZJControlsClass
//
//  Created by mac on 2024/12/8.
//

#import "HomeTransforShowOrderInfoViewController.h"
#import "HomeTransforShowOrderInfoSecondOneTableViewCell.h"
#import "HomeTransforShowOrderInfoSecondTwoTableViewCell.h"
#import "HomeTransforShowOrderInfoThreeTableViewCell.h"
#import "HomeTransforOrderNetWorking.h"
#import "HomeTransferShowOrderInfoModel.h"


@interface HomeTransforShowOrderInfoViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UILabel * resultLabel;
@property (nonatomic,strong) UITableView * tableView;
@property (nonatomic,strong) NSMutableArray * dataSource;

@property (nonatomic,strong) HomeGoodsTransferApplyDetailNetworking * manager;
@property (nonatomic,strong) HomeTransferShowOrderInfoModel * model;

@end

@implementation HomeTransforShowOrderInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"转单申请详情";
    [self loadNet];
    
    [self.view addSubview:self.resultLabel];
    [self.view addSubview:self.tableView];
    
    [self.resultLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).with.offset(StatusBarHeight);
        make.left.right.equalTo(self.view);
        make.height.equalTo(@(ALIGN_HEIGHT_PT(36)));
    }];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.top.equalTo(self.resultLabel.mas_bottom);
    }];
    
}



-(void)loadNet{
    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.label.text = @"加载中...";
    [self.manager dispatchTaskWithParam:@{@"transferId":self.ID} completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
        [hud hideAnimated:YES];
        if (responseObject && [responseObject[@"code"] intValue] == 200){
            
            self.model = [HomeTransferShowOrderInfoModel mj_objectWithKeyValues:responseObject[@"data"]];
            // 基础信息
            [self.dataSource addObject:@[self.model]];
            //工具货物信息
            if (self.model.details.count > 0){
                [self.dataSource addObject:self.model.details];
            }
            if (self.model.photo.count > 0){
                [self.dataSource addObject:self.model.photo];
            }
            
            
            [self.tableView reloadData];
            
        }else{
            [LUtilityTools showAlertWithTitle:@"温馨提示" message:responseObject[@"msg"] buttonAction:nil];
            return;
        }
    }];
    
    
    
    
}



#pragma mark - UITableViewDelegate & UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataSource.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.dataSource[section] count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // 根据不同的cell类型返回不同的cell
    if (indexPath.section == 0) {
        // 第一种cell
        HomeTransforShowOrderInfoSecondOneTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HomeTransforShowOrderInfoSecondOneTableViewCell"];
        if (!cell) {
            cell = [[HomeTransforShowOrderInfoSecondOneTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"HomeTransforShowOrderInfoSecondOneTableViewCell"];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        HomeTransferShowOrderInfoModel * model = self.dataSource[indexPath.section][indexPath.row];
        cell.model = model;
        
        return cell;
    } else if (indexPath.section == 1 && self.model.details.count > 0){
        // 第二种cell
        HomeTransforShowOrderInfoSecondTwoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HomeTransforShowOrderInfoSecondTwoTableViewCell"];
        if (!cell) {
            cell = [[HomeTransforShowOrderInfoSecondTwoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"HomeTransforShowOrderInfoSecondTwoTableViewCell"];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        HomeTransforOrderInfoAreaListModel * model = self.dataSource[indexPath.section][indexPath.row];
        cell.model = model;
        
        return cell;
    }else if (indexPath.section == 2 && self.model.photo.count > 0){
        HomeTransforShowOrderInfoThreeTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"HomeTransforShowOrderInfoThreeTableViewCell"];
        if (!cell){
            cell = [[HomeTransforShowOrderInfoThreeTableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"HomeTransforShowOrderInfoThreeTableViewCell"];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        NSString * img =  self.dataSource[indexPath.section][indexPath.row];
        cell.imgUrl = img;
        
        
        return cell;
    }
    return [UITableViewCell new];

}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, ALIGN_HEIGHT_PT(45))];
        headerView.backgroundColor = [UIColor whiteColor];
        
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.text = @"基础信息";
        titleLabel.textColor = [UIColor blackColor];
        titleLabel.font = FONT14;
        [headerView addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(headerView).offset(ALIGN_WIDTH_PT(12));
            make.centerY.equalTo(headerView);
        }];
        
        return headerView;
    } else if (section == 1) {
        UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, ALIGN_HEIGHT_PT(90))];
        headerView.backgroundColor = [UIColor whiteColor];
        
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.text = @"工具货物信息";
        titleLabel.textColor = [UIColor blackColor];
        titleLabel.font = FONT14;
        [headerView addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(headerView);
            make.left.equalTo(headerView).offset(ALIGN_WIDTH_PT(12));
            make.height.equalTo(@(ALIGN_HEIGHT_PT(45)));
        }];
        
        CGFloat width = SCREEN_WIDTH / 3;
        
        for (int i = 0; i < 3; i++) {
            UILabel *label = [[UILabel alloc] init];
            label.textAlignment = NSTextAlignmentCenter;
            label.backgroundColor = COLOR_RGB(244, 244, 244);
            label.font = FONT14;
            label.textColor = [UIColor blackColor];
            if (i == 0) {
                label.text = @"货物名称";
            } else if (i == 1) {
                label.text = @"规格尺寸";
            } else {
                label.text = @"数量";
            }
            [headerView addSubview:label];
            [label mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(titleLabel.mas_bottom);
                make.left.equalTo(headerView).offset(width * i);
                make.width.mas_equalTo(width);
                make.height.equalTo(@(ALIGN_HEIGHT_PT(45)));
            }];
        }
        
        return headerView;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return ALIGN_HEIGHT_PT(45);
    }else if (section == 1){
        return ALIGN_HEIGHT_PT(90);
    }
    return 0;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return ALIGN_HEIGHT_PT(315);
    } else if (indexPath.section == 1) {
        return ALIGN_HEIGHT_PT(45);
    }
    return ALIGN_HEIGHT_PT(207);
}

#pragma mark - Lazy Loading
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor whiteColor];
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
    }
    return _tableView;
}

-(UILabel *)resultLabel{
    if (!_resultLabel){
        _resultLabel = [UILabel new];
        _resultLabel.text = @"处理结果：接单人同意转单";
        _resultLabel.textColor = COLOR_RGB(50, 113, 237);
        _resultLabel.font = FONT12;
        _resultLabel.textAlignment = NSTextAlignmentCenter;
        _resultLabel.backgroundColor = COLOR_RGBA(50, 113, 237, 0.1);
    }
    return _resultLabel;
}

- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}


-(HomeGoodsTransferApplyDetailNetworking *)manager{
    if (!_manager){
        _manager = [HomeGoodsTransferApplyDetailNetworking new];
    }
    return _manager;
}


@end
